package offer.leetcode.easy;

/**
 *  替换空格
 *  https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/
 * @author DengYuan2
 * @create 2021-01-04 20:41
 */
public class E_05 {
    public static void main(String[] args) {
        String s = " We are happy. ";
//        String result = replaceSpace(s);
//        System.out.println(result);
        String space2 = replaceSpace2(s);
        System.out.println(space2);

    }

    //我的写法，虽然通过，但是用的是java自带的函数
    public static String replaceSpace(String s) {
        return s.replace(" ", "%20");
    }

    /**
     * 遍历添加
     * 更快
     * @param s
     * @return
     */
    public static String replaceSpace2(String s){
        StringBuilder builder = new StringBuilder();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i]==' '){
                builder.append("%20");
            }else {
                builder.append(chars[i]);
            }

        }
        return builder.toString();
    }
}
